package com.itheima.demo6collection;

import java.util.ArrayList;
import java.util.Iterator;

//并发修改异常
public class CollectionTraversalDemo5 {
    public static void main(String[] args) {
        ArrayList<String> c = new ArrayList<>();
        c.add("java基础");
        c.add("java-we");
        c.add("黑枸杞");
        c.add("javaee");
        c.add("红枸杞");
       for(int i=0;i<c.size();i++)
       {
           String s = c.get(i);
           //包含枸杞
           if(s.contains("枸杞"))
           {
               c.remove(s);
               i--;
           }
       }
        System.out.println(c);
        //迭代器可以解决并发修改异常
        Iterator<String> it = c.iterator();
        while(it.hasNext()){
            String s = it.next();
            if(s.contains("枸杞")){
                //用迭代器本身的remove方法删除
                it.remove();
            }
        }
    }

    //增强for和lambda表达式不能解决并发修改异常
    //结论 for和lambda只适合做遍历 不适合做修改
}
